<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.userType" type="card" @tab-click="handleQuery">
      <el-tab-pane v-for="(i) in userTypeList" :key="i.value" :label="i.label" :name="i.value" />
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" inline label-width="80px" @submit.native.prevent>
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="名称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="注册时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>{{ scope.row.status == 3 ? "待审核" : scope.row.status == 4 ? "审核拒绝" : '审核通过' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button plain icon="el-icon-view" size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="success" plain icon="el-icon-check" size="mini" @click="handleAudit(0, scope.row)"
            v-hasPermi="['system:user:remove']" v-if="scope.row.status == 3">审核通过</el-button>
          <el-button type="danger" plain icon="el-icon-close" size="mini" @click="handleAudit(4, scope.row)"
            v-hasPermi="['system:user:remove']" v-if="scope.row.status == 3">审核拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <User-detail-dialog v-model="detailShow" :row="detailRow" />
  </div>
</template>

<script>
import {
  listAuditList,
  postAdit
} from "@/api/system/audit";
import {
  getUser
} from "@/api/system/user";
import UserDetailDialog from "@/views/system/audit/cp/UserDetailDialog.vue"

export default {
  name: "Template",
  components: {
    UserDetailDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户列表
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null, // 用户姓名
        phonenumber: null, // 手机号码
        userType: "00", // 用户类型
      },
      userTypeList: [{
        value: "00",
        label: "系统用户"
      },
      {
        value: "01",
        label: "消防技术服务机构"
      },
      {
        value: "02",
        label: "委托单位"
      },
      {
        value: "03",
        label: "一级消防注册工程师"
      },
      {
        value: "04",
        label: "中级消防设施操作员"
      },
      {
        value: "05",
        label: "平台维修员"
      },
      ],
      detailShow: false, // 详情弹窗
      detailRow: {}, // 用户信息
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板管理列表 */
    getList() {
      this.loading = true;
      listAuditList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.multiple = !selection.length;
    },
    // 审核用户
    handleAudit(status, row) {
      if (status == 0) {
        this.submitForm(status, row);
      } else {
        this.$prompt("拒绝原因", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPattern: /\S/,
          inputErrorMessage: '拒绝原因不能为空'
        }).then(({
          value
        }) => {
          this.submitForm(status, row, value);
        });
      }
    },
    // 提交审核结果
    submitForm(status, row, reason) {
      this.$modal
        .confirm(
          `确认${status == 0 ? "审核通过" : "审核拒绝"}该信息？`
        )
        .then(() => {
          postAdit({
            status,
            userId: row.userId,
            reasonForRefusal: status == 0 ? null : reason,
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("审核成功");
          });
        })
        .catch(() => { });
    },
    // 查看用户详情
    handleDetail(row) {
      this.detailRow = {};
      getUser(row.userId).then(res => {
        console.log(res.data, '>>>>>4468');
        if (res.code == 200) {
          this.detailRow = res.data;
          this.detailShow = true;
        }
      })
    },
  },
};

</script>
